package com.demo.blog;

import com.demo.common.model.Blog;
import com.jfinal.aop.Before;
import com.jfinal.mvc.ActionKey;
import com.jfinal.mvc.Controller;

/**
 * BlogController
 * 所有 sql 与业务逻辑写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
 */
@Before(BlogInterceptor.class)
public class BlogController extends Controller {

	@ActionKey("{page}")
	public void index(int page) {
		setAttr("blogPage", Blog.me.paginate(page, 10));
		render("blog.html");
	}

	public void add() {
	}

//	@Before(BlogValidator.class)
	public void save(Blog blog) {
		blog.save();
		redirect("/blog/1");
	}

	@ActionKey("edit/{id}")
	public void edit(int id) {
		setAttr("blog", Blog.me.findById(id));
	}

//	@Before(BlogValidator.class)
	public void update(Blog blog) {
		blog.update();
		redirect("/blog/1");
	}

	@ActionKey("delete/{id}")
	public void delete(int id) {
		Blog.me.deleteById(id);
		redirect("/blog/1");
	}
}
